package com.china08.yxyapi.api.manage.tec.teaching.weikecheng.weike;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.es.ElasticYxyResResource;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.service.yxweike.YxyWeikeService;

/**
 * 老师 ---我的微课 --查询
 * 
 */
@CrossOrigin
@RestController
public class WeikechengSrhController extends YxyApiBaseController {

    @Autowired
    private YxyWeikeService weikeService;

    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/tec/teaching/weikecheng/weike/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel req) {
        String userId = getProfile(request, response).getId();
        // 查询我的微课
        Pageable pageable = req.getPageable(Direction.DESC, "uploadTime");
        Page<ElasticYxyResResource> page = weikeService.findMyWeike(req.stage, req.subject, req.viewScope, req.status,
                req.keyword.toLowerCase(), userId, pageable);
        List<RespModel> list = new ArrayList<RespModel>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for (ElasticYxyResResource elasticYxyResResource : page) {
            RespModel respModel = new RespModel();
            respModel.weikeId = elasticYxyResResource.getId();
            respModel.cover = elasticYxyResResource.getCover();
            respModel.status = elasticYxyResResource.getStatus();
            respModel.title = elasticYxyResResource.getTitle();
            // 分类
            List<String> categoryList = new ArrayList<String>();
            String category = "";
            if (StringUtils.isNotBlank(elasticYxyResResource.getStageName())) {
                categoryList.add(elasticYxyResResource.getStageName());
            }

            if (StringUtils.isNotBlank(elasticYxyResResource.getWeikeSubjectName())) {
                categoryList.add(elasticYxyResResource.getWeikeSubjectName());
            }

            if (CollectionUtils.isNotEmpty(elasticYxyResResource.getKnowNames())) {
                categoryList.addAll(elasticYxyResResource.getKnowNames());
            }
            category = StringUtils.join(categoryList, ",");
            respModel.category = category;
            String viewScope = "";
            if (elasticYxyResResource.getViewScope() == 2) {
                viewScope = "共享到教育云";
            }
            if (elasticYxyResResource.getViewScope() == 1) {
                viewScope = "分享到学校";
            }
            if (elasticYxyResResource.getViewScope() == 0) {
                viewScope = "私有";
            }
            respModel.viewScope = viewScope;
            respModel.viewStatus = elasticYxyResResource.getViewScope();
            respModel.uploadTime = sdf.format(elasticYxyResResource.getUploadTime());
            respModel.remark = elasticYxyResResource.getRemark();
            list.add(respModel);
        }
        Page<RespModel> pageImpl = new PageImpl<RespModel>(list, pageable, page.getTotalElements());
        return pageImpl;
    }

    protected static class ReqModel extends YxyApiPageReqModel {

        public String stage;// 学段

        public String subject;// 科目

        public Integer viewScope;// 共享范围 0 私有,1学校,3教育云

        public Integer status;// 审核状态 0：待审核 1：已通过 2：未通过 3：已下架

        public String keyword;// 微课名称

    }

    protected static class RespModel extends YxyApiRespModel {

        public String weikeId;// 微课Id

        public Integer status;// 审核状态 0：待审核 1：已通过 2：未通过 3：已下架

        public String cover;// 封面

        public String title;// 标题

        public String category;// 知识点,知识点...

        public String viewScope;// 共享范围

        public String uploadTime;// 上传时间

        public Integer viewStatus;// 共享范围的状态码 0 私有 ,1 学校,2教育云

        public String remark;// 原因

    }
}
